

/*消费者管理模块功能测试*/

#include "../mqserver/mq_consumer.hpp"
#include <gtest/gtest.h>

xgmq::ConsumerManager::ptr cmp;

// 编写全局测试套件
class ConsumerTest : public testing::Environment
{
public:
    void SetUp() override
    {
        // 初始化环境
        cmp = std::make_shared<xgmq::ConsumerManager>();
        cmp->initQueueConsumer("队列1");
    }
    void TearDown() override
    {
        // 清理环境
        cmp->clear();
    }
};
void cb(const std::string &tag, const xgmq::basicProperties &bp, const std::string &body)
{
    std::cout << tag << std::endl;
}
// 新增功能测试
TEST(Consumer, insert_test)
{
    cmp->create("消费者1", "队列1", false, cb);
    cmp->create("消费者2", "队列1", false, cb);
    cmp->create("消费者3", "队列1", false, cb);

    ASSERT_EQ(cmp->isExists("队列1", "消费者1"), true);
    ASSERT_EQ(cmp->isExists("队列1", "消费者2"), true);
    ASSERT_EQ(cmp->isExists("队列1", "消费者3"), true);
}

// 删除功能测试
TEST(Consumer, remove_test)
{
    cmp->remove("队列1", "消费者1");

    ASSERT_EQ(cmp->isExists("队列1", "消费者1"), false);
    ASSERT_EQ(cmp->isExists("队列1", "消费者2"), true);
    ASSERT_EQ(cmp->isExists("队列1", "消费者3"), true);
}

// 获取功能测试
TEST(Consumer, choose_test)
{
    xgmq::Consumer::ptr con = cmp->choose("队列1");
    ASSERT_NE(con.get(), nullptr);
    ASSERT_EQ(con->tag, "消费者2");

    con = cmp->choose("队列1");
    ASSERT_NE(con.get(), nullptr);
    ASSERT_EQ(con->tag, "消费者3");
}
int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    // 添加全局测试套件
    testing::AddGlobalTestEnvironment(new ConsumerTest);
    int ret = RUN_ALL_TESTS();

    return 0;
}
